import Vue from 'vue'
import vuex from 'vuex'

Vue.use(vuex)

export default new vuex.Store({
    state:{
        lists: localStorage.getItem('lists') ? JSON.parse(localStorage.getItem('lists')) : []
    },
    getters: {

    },
    mutations: {
        //改变待办事项状态
        change(state,val) {
            let index = state.lists.findIndex(item => item.id === val)
            state.lists[index].finished = !state.lists[index].finished
            localStorage.setItem('lists', JSON.stringify(state.lists))
        },
        //删除每一项
        deleteItem(state,val) {
            let index = state.lists.findIndex(item => item.id === val)
            state.lists.splice(index, 1)
            localStorage.setItem('lists', JSON.stringify(state.lists))
        },
        //添加待办事项
        add(state,val) {
            let obj = {
                val: val,
                finished: false,
                id: new Date().getTime()
            }
            state.lists.unshift(obj)
            localStorage.setItem('lists', JSON.stringify(state.lists))
        }
    }
})